﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BAL.Components;
using BAL.Entities;
using DigitalAnts.App_Code.BAL.Components;
using DigitalAnts.App_Code.BAL.Utils;

namespace DigitalAnts.Admin
{
    public partial class ProductManager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ShowProduct();
                showDropUser();
                showDropProducer();
                showDropFuel();
                showDropCity();
            }
        }
        public void ShowProduct()
        {
            gv_product.DataSource = clsProductBAL.doProductSelectList();
            gv_product.DataBind();
        }

        public void addNew()
        {
            clsProduct obj = new clsProduct();
            obj.ProductName = txtproduct.Text;
            obj.Avatar = "~/skin/productimage/" + fulavatar.FileName;
            obj.Userid = Convert.ToInt32(ddluser.SelectedValue);
            obj.AntiThiefDevice = Convert.ToInt32(txtdevice.Text);
            obj.Color = txtcolor.Text;
            obj.IsNew = Convert.ToInt32(txtnew.Text);
            obj.ProducerID = Convert.ToInt32(ddlproducer.SelectedValue);
            obj.Price = Convert.ToDouble(txtprice.Text);
            obj.KilometersTraveled = int.Parse(txtkilometer.Text);
            obj.FuelID = Convert.ToInt32(ddlfuel.SelectedValue);
            obj.CityID = Convert.ToInt32(ddlcity.SelectedValue);
            if (clsProductBAL.doAddNewProduct(obj) == true && clsUploadImage.uploadProductImage(fulavatar) == true)
            {
                lblmess.Text = "Add new success";
            }
            else { lblmess.Text = "Add new fail"; }
        }
        public void showDropUser()
        {
            ddluser.DataSource = clsUserBAL.doGetAllUser();
            ddluser.DataTextField = "colUserName";
            ddluser.DataValueField = "colUserID";
            ddluser.DataBind();
        }
        public void showDropProducer()
        {
            ddlproducer.DataSource = clsProducerBAL.doProducerSelectList();
            ddlproducer.DataTextField = "colProducer";
            ddlproducer.DataValueField = "colProducerID";
            ddlproducer.DataBind();
        }
        public void showDropFuel()
        {
            ddlfuel.DataSource = clsFuelBAL.doFuelSelectList();
            ddlfuel.DataTextField = "Fuel";
            ddlfuel.DataValueField = "FuelID";
            ddlfuel.DataBind();
        }
        public void showDropCity()
        {
            ddlcity.DataSource = clsCityBAL.doCitySelectList();
            ddlcity.DataTextField = "CityName";
            ddlcity.DataValueField = "CityID";
            ddlcity.DataBind();
        }

        protected void bttAdd_Click(object sender, EventArgs e)
        {
            addNew();
            ShowProduct();
        }

        protected void gv_product_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gv_product.PageIndex = e.NewPageIndex;
            gv_product.DataBind();
        }

       
    }
}